/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agh.air.bpel2bpmn.walker;

import org.agh.air.bpel2bpmn.exception.ImproperInputFileException;
import org.agh.air.bpel2bpmn.util.Util;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author paletz
 */
public class XmlWalker {
    
    public String walk(Node node, Document doc) throws ImproperInputFileException {
        
        Node root = node;
       
         NodeList nl = node.getChildNodes();

            for(int i=0; i<nl.getLength(); i++){
                if(nl.item(i).hasChildNodes()){
                   walk(nl.item(i), doc);
                }
            }

            String str = ComparatorInvoker.invoke(node);
            System.out.println(Util.specialCharReplacer(str));
            if(!node.getParentNode().getNodeName().equals("#document")){
                node.getParentNode().replaceChild(doc.createElement(str), node );
            } else 
                return str;
                    
            for(int i=0; i<nl.getLength(); i++){
                node.removeChild(nl.item(i));
            }
            if(root.getParentNode()==null)
                return root.getNodeName();            
            node.getParentNode().removeChild(node);
        
            return root.getNodeName();
    }
    
}
